package com.msb.strategy.filter;

import com.msb.common.constart.CacheConstant;
import com.msb.common.model.StandardSubmit;
import com.msb.strategy.feign.BeaconCacheClient;
import com.msb.strategy.filter.StrategyFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 策略模式过滤器上下文
 *
 * @Author: lmf
 * @Create: 2025/3/11 22:49
 * @module
 */
@Component
@Slf4j
public class StrategyFilterContext {
    @Resource
    private Map<String, StrategyFilter> strategyFilterMap;

    @Resource
    private BeaconCacheClient cacheClient;

    public void strategy(StandardSubmit submit) {
        String CLIENT_FILTERS = "clientFilters";
        // 获取redis中对应需要校验的策略
        final String filters = cacheClient.hGet(CacheConstant.CLIENT_BUSINESS + submit.getApiKye(), CLIENT_FILTERS);
        String[] filterArr;
        if (filters != null && ((filterArr = filters.split(",")).length > 0)) {
            for (String filter : filterArr) {
                if (strategyFilterMap.get(filter) == null) {
                    log.error("没有找到对应的策略" + filter);
                    continue;
                }
                strategyFilterMap.get(filter).strategy(submit);
            }
        }
    }
}
